package com.clive.llweb.controller;

import com.clive.llweb.bean.Dishes;
import com.clive.llweb.service.DishService;
import com.clive.llweb.vo.DishResult;
import com.clive.llweb.vo.DishesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
//spring mvc 注解  可以接收请求生成响应
@RestController
//url总路径
@RequestMapping("/dish")
//跨域
@CrossOrigin
public class DishController {
    //   从spring容器里面把接口的唯一子类拿出来给service  所以不用人为new
    @Autowired
    private DishService dishesService;
    @RequestMapping("/addDishes")
//                                           校验用户输入的值
    public DishResult addDishes(@RequestBody @Valid Dishes dishes){
        boolean b = dishesService.addDishes(dishes);
        if (b){
            return DishResult.ok();
        }
        return DishResult.error("添加菜品失败");
    }
    @RequestMapping("/findOne/{id}")
//                                    映射url地址中的占位符
    public DishResult findDishesById(@PathVariable Integer id){
         Dishes dishes= dishesService.findDishesById(id);
//         返回一个集合
         return DishResult.ok().date("dishes",dishes);
    }
    @RequestMapping("/update")
//                                           校验用户输入的值是否正确
    public DishResult updateDish(@RequestBody @Valid Dishes dishes) {
        boolean b = dishesService.updateDish(dishes);
        if (b) {
            return DishResult.ok();
        }
        return DishResult.error("修改菜品失败");
    }
    @RequestMapping("/delete/{id}")
    public DishResult deleteDish(@PathVariable Integer id){
        boolean b = dishesService.deleteDish(id);
        if (b) {
            return DishResult.ok();
        }
        return DishResult.error("删除菜品失败");
    }
//    多条件查询  jsr303不用起作用
    @RequestMapping("/query")
    public DishResult queryPage(@RequestBody Dishes dishes){
        DishesVo vo = dishesService.queryDish(dishes);
        return DishResult.ok().date("vo",vo);
    }
}
